package com.question.business.dept.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.question.business.dept.model.ao.*;
import com.question.business.dept.model.vo.DeptAddVO;
import com.question.business.dept.model.vo.DeptDetailVO;
import com.question.business.dept.model.vo.DeptDropDownVO;
import com.question.business.dept.model.vo.DeptVO;
import com.question.business.dept.service.IDeptService;
import com.question.common.annotation.DataConvert;
import com.question.common.utils.DateUtil;
import lombok.AllArgsConstructor;
import org.apache.commons.compress.utils.IOUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;


/**
 * 部门接口
 *
 * @author star
 * @since 2023-09-14
 */
@RestController
@AllArgsConstructor
@RequestMapping("/dept")
public class DeptController {

    private final IDeptService service;

    /**
     * 部门分页查询
     *
     * @param ao 请求参数
     * @return Page<DeptVO> 响应参数
     */
    @GetMapping("/page")
    public Page<DeptVO> queryDeptPage(@Validated DeptPageAO ao) {
        return service.queryDeptPage(ao);
    }

    /**
     * 部门明细查询
     *
     * @param id 部门ID
     * @return DeptDetailVO 响应参数
     */
    @DataConvert
    @GetMapping("/detail")
    public DeptDetailVO queryDeptDetail(@Validated @NotBlank(message = "删除数据不能为空") @Size(max = 20, message = "ID最大长度不能超过20") String id) {
        return service.queryDeptDetail(id);
    }

    /**
     * 部门新增数据
     *
     * @param ao 请求参数
     * @return DeptAddVO 响应参数
     */
    @PostMapping("/add")
    public DeptAddVO addDept(@Validated @RequestBody DeptAddAO ao) {
        return service.addDept(ao);
    }

    /**
     * 部门修改数据
     *
     * @param ao 数据
     */
    @PutMapping("/update")
    public void updateDept(@Validated @RequestBody DeptUpdateAO ao) {
        service.updateDept(ao);
    }

    /**
     * 部门删除数据
     *
     * @param id 部门ID
     */
    @DeleteMapping("/delete")
    public void deleteDept(@Validated @NotBlank(message = "删除数据不能为空") @Size(max = 20, message = "ID最大长度不能超过20") String id) {
        service.deleteDept(id);
    }

    /**
     * 部门批量删除数据
     *
     * @param ids 部门ID
     */
    @DeleteMapping("/batch/delete")
    public void deleteBatchDept(@RequestParam @Validated @NotNull(message = "删除数据不能为空") @Size(min = 1, message = "删除数据不能为空") List<@NotBlank(message = "删除数据不能为空") @Size(max = 20, message = "ID最大长度不能超过20") String> ids) {
        service.deleteBatchDept(ids);
    }

    /**
     * 部门导出数据
     *
     * @param ao       请求参数
     * @param response 响应流
     */
    @GetMapping("/export")
    public void exportDept(DeptPageAO ao, HttpServletResponse response) throws IOException {
        response.reset();
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("部门-" + DateUtil.getStringTime() + ".xlsx", "UTF-8"));
        ServletOutputStream outputStream = response.getOutputStream();
        service.exportDept(ao, outputStream);
        IOUtils.closeQuietly(outputStream);
    }

    /**
     * 部门启用、停用
     *
     * @param ao 请求参数
     */
    @PutMapping("/enable")
    public void enableDept(@Validated @RequestBody DeptEnabledAO ao) {
        service.enableDept(ao);
    }

    /**
     * 部门下拉列表查询
     *
     * @param ao 请求参数
     * @return List<DeptDropDownVO> 响应参数
     */
    @GetMapping("/drop/down")
    public List<DeptDropDownVO> queryDeptDropDown(@Validated DeptDropDownAO ao) {
        return service.queryDeptDropDown(ao);
    }

}
